package com.tuananh.radnom.model;

import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


import com.tuananh.radnom.model.netservice.MyMapTypeAdapter;
import com.tuananh.radnom.model.netservice.NetService;

/**
 * 
 * @author SilentSkyTA A NetNode object represents a network device such as
 *         desktop, router, switch ....
 */
@XmlRootElement(name = "node")
public class NetNode extends NetElement {
	/**
	 * List of NICs
	 */

	ArrayList<NetInterface> netInterfaces;
	// ArrayList<NetService> netServices;
	HashMap<String, String> techInfo;
	HashMap<String, String> geoInfo;
	String techoInfoString;
	@XmlElementWrapper(name = "netInterfaces")
	@XmlElement(name = "interface")
	public ArrayList<NetInterface> getNetInterfaces() {
		if (netInterfaces == null)
			netInterfaces = new ArrayList<NetInterface>();
		return netInterfaces;
	}

	public void setNetInterfaces(ArrayList<NetInterface> netInterfaces) {
		this.netInterfaces = netInterfaces;
	}

	/*
	 * public ArrayList<NetService> getNetServices() { if (netServices == null)
	 * netServices = new ArrayList<NetService>(); return netServices; }
	 * 
	 * public void setNetServices(ArrayList<NetService> netServices) {
	 * this.netServices = netServices; }
	 */
	@XmlJavaTypeAdapter(MyMapTypeAdapter.class)
	public HashMap<String, String> getTechInfo() {
		if (techInfo == null)
			techInfo = new HashMap<String, String>();
		return techInfo;
	}

	public void setTechInfo(HashMap<String, String> techInfo) {
		this.techInfo = techInfo;
	}
	@XmlTransient
	public String getTechoInfoString(){
		return techInfo.toString();
	}
	@XmlJavaTypeAdapter(MyMapTypeAdapter.class)
	public HashMap<String, String> getGeoInfo() {
		if (geoInfo == null)
			geoInfo = new HashMap<String, String>();
		return geoInfo;
	}

	public void setGeoInfo(HashMap<String, String> geoInfo) {
		this.geoInfo = geoInfo;
	}

	public String test;

	public boolean validate(Object ne) {
		// TODO Auto-generated method stub
		NetNode node = (NetNode) ne;
		try {
			if (node.name.equals(this.name))
				return false;
		} catch (NullPointerException nullException) {
			return false;
		}
		return true;
	}

	public boolean assign(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof NetNode) {
			try {
				NetNode node = (NetNode) obj;
				super.assign(obj);
				// first trial assign for error test
				// assign to the real object
				this.geoInfo = node.geoInfo;
				this.techInfo = node.techInfo;
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}

	public static NetInterface findNetInt(String name,
			ArrayList<NetInterface> list) {
		for (NetInterface netInt : list)
			if (netInt.getName().equals(name))
				return netInt;
		return null;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof NetNode){
			if (((NetNode)obj).getName().equals(this.getName()))
				return true;
		}
		return false;
	}
}
